import api from '@/utils/api'

/**
 * 生成数据库备份
 * @returns {Promise}
 */
export function backupDatabase() {
  return api({
    url: '/migration/backup',
    method: 'post',
    timeout: 300000 // 5分钟超时，数据库备份可能需要较长时间
  })
}

/**
 * 恢复数据库
 * @param {FormData} formData 包含SQL备份文件的表单数据
 * @returns {Promise}
 */
export function restoreDatabase(formData) {
  return api({
    url: '/migration/restore',
    method: 'post',
    data: formData,
    headers: {
      'Content-Type': 'multipart/form-data'
    },
    timeout: 300000 // 5分钟超时，数据库恢复可能需要较长时间
  })
}

/**
 * 获取备份历史记录
 * @param {Object} params 查询参数
 * @returns {Promise}
 */
export function getBackupHistory(params) {
  return api({
    url: '/migration/backup-history',
    method: 'get',
    params
  })
}

/**
 * 下载备份文件
 * @param {String} fileName 备份文件名
 * @returns {Promise}
 */
export function downloadBackup(fileName) {
  return api({
    url: `/migration/download/${fileName}`,
    method: 'get',
    responseType: 'blob'
  })
}

/**
 * 删除备份文件
 * @param {String} fileName 备份文件名
 * @returns {Promise}
 */
export function deleteBackup(fileName) {
  return api({
    url: `/migration/backup/${fileName}`,
    method: 'delete'
  })
}

export default {
  backupDatabase,
  restoreDatabase,
  getBackupHistory,
  downloadBackup,
  deleteBackup
}